Skip to content

Method: static {...}

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.model.*;
18: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
19: import cz.cvut.kbss.ontodriver.owlapi.util.MutableAddAxiom;
20: import cz.cvut.kbss.ontodriver.owlapi.util.MutableRemoveAxiom;
21: import org.semanticweb.owlapi.model.*;
22: import org.semanticweb.owlapi.reasoner.OWLReasoner;
23: import org.semanticweb.owlapi.search.EntitySearcher;
24:
25: import java.net.URI;
26: import java.util.ArrayList;
27: import java.util.Collection;
28: import java.util.List;
29: import java.util.Set;
30: import java.util.stream.Collectors;
31:
32: class TypesHandler {
33:
34: private final OWLOntology ontology;
35: private final OWLDataFactory dataFactory;
36: private final OWLReasoner reasoner;
37: private final OntologySnapshot snapshot;
38:
39: private final OwlapiAdapter adapter;
40:
41: TypesHandler(OwlapiAdapter adapter, OntologySnapshot snapshot) {
42: this.adapter = adapter;
43: this.snapshot = snapshot;
44: this.ontology = snapshot.getOntology();
45: this.dataFactory = snapshot.getDataFactory();
46: this.reasoner = snapshot.getReasoner();
47: }
48:
49: Set<Axiom<URI>> getTypes(NamedResource subject, Collection<URI> contexts, boolean includeInferred) {
50: final Collection<? extends OWLClassExpression> owlClasses;
51: if (!includeInferred) {
52: owlClasses = loadExplicitClasses(subject);
53: } else {
54: owlClasses = inferClasses(subject);
55: }
56: return owlClassesToAxioms(subject, includeInferred, owlClasses);
57: }
58:
59: private Collection<OWLClassExpression> loadExplicitClasses(NamedResource subject) {
60: return EntitySearcher.getTypes(getIndividual(subject), ontology.importsClosure()).collect(Collectors.toSet());
61: }
62:
63: private OWLNamedIndividual getIndividual(NamedResource subject) {
64: return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
65: }
66:
67: private static Set<Axiom<URI>> owlClassesToAxioms(NamedResource subject, boolean inferred,
68: Collection<? extends OWLClassExpression> owlClasses) {
69: return owlClasses.stream().map(expr -> new AxiomImpl<>(subject,
70: Assertion.createClassAssertion(inferred), new Value<>(expr.asOWLClass().getIRI().toURI())))
71: .collect(Collectors.toSet());
72: }
73:
74: private Collection<? extends OWLClassExpression> inferClasses(NamedResource subject) {
75: final OWLNamedIndividual individual = getIndividual(subject);
76: return reasoner.getTypes(individual, false).entities().collect(Collectors.toSet());
77: }
78:
79: void addTypes(NamedResource subject, URI context, Set<URI> types) {
80: assert !types.isEmpty();
81:
82: final List<OWLAxiom> axioms = getOwlAxiomsForTypes(subject, types);
83: final List<OWLOntologyChange> changes = axioms.stream().map(axiom -> new MutableAddAxiom(ontology, axiom))
84: .collect(Collectors.toList());
85:
86: adapter.addTransactionalChanges(snapshot.applyChanges(changes));
87: }
88:
89: private List<OWLAxiom> getOwlAxiomsForTypes(NamedResource subject, Set<URI> types) {
90: final List<OWLAxiom> axioms = new ArrayList<>(types.size());
91: final OWLNamedIndividual individual = getIndividual(subject);
92: axioms.addAll(types.stream().map(type -> dataFactory
93: .getOWLClassAssertionAxiom(dataFactory.getOWLClass(IRI.create(type)), individual))
94: .collect(Collectors.toList()));
95: return axioms;
96: }
97:
98: void removeTypes(NamedResource subject, URI context, Set<URI> types) {
99: assert !types.isEmpty();
100:
101: final List<OWLAxiom> axioms = getOwlAxiomsForTypes(subject, types);
102: final List<OWLOntologyChange> changes = axioms.stream().map(axiom -> new MutableRemoveAxiom(ontology, axiom))
103: .collect(Collectors.toList());
104:
105: adapter.addTransactionalChanges(snapshot.applyChanges(changes));
106: }
107: }